package com.sctu.EMSApp;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;


public class MyActivity extends BaseActivity implements View.OnClickListener {
    /**
     * Called when the activity is first created.
     */
    // 定义一个变量，来标识是否退出
    private static boolean isExit = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        setBottom();
        setDefaultFragment();

    }

    protected void setDefaultFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_contents, scenicFragment);
        fragmentTransaction.commit();
        buttomScenicImage.setBackgroundResource(R.drawable.bottom_scenic_tour_on);
        buttomPlayImage.setBackgroundResource(R.drawable.bottom_fun_emei);
        buttomGuideImage.setBackgroundResource(R.drawable.bottom_wisdom_guides);
        buttomMyinfoImage.setBackgroundResource(R.drawable.bottom_myinfo);
    }

    /**
     * 按2次退出
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

}